package service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import common.PageReturn;
import dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pojo.EssayAndUser;
import pojo.Manager;
import pojo.MessageUserEssay;
import pojo.Page;

@Service
public class ManagerServiceImpl implements ManagerServiceI {

    @Autowired
    private ManagerDao managerDao;
    @Autowired
    private ManagerMessageMapper managerMessageMapper;
    @Autowired
    private EssayDao essayDao;
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public boolean login(String managerName, String password, HttpServletRequest request) {
        Manager manager = managerDao.selectByManagerName(managerName);
        if (manager == null) {//查无此人
            return false;
        } else if (password.equals(manager.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute("manager", manager);
            return true;
        } else//密码错误
            return false;
    }

    @Override
    public List<MessageUserEssay> FindAllMessage(Page page) {
        PageReturn pageReturn=new PageReturn();
        page=pageReturn.ReturnPage(page);
        List<MessageUserEssay> list = managerDao.selectAllMessage(page);
        return list;
    }

    @Override
    public boolean RemoveMessage(Integer managerMessageId) {
        int i = managerMessageMapper.deleteByPrimaryKey(managerMessageId);
        if (i > 0) {
            return true;
        } else
            return false;
    }

    @Override
    public List<EssayAndUser> FindByAuditing(String flag,Page page) {
        if ("1".equals(flag)) {
            Integer auditing = 1;
            PageReturn pageReturn=new PageReturn();
            page=pageReturn.ReturnPage(page);
            System.out.println(page.getCurrentPage());
            List<EssayAndUser> list = essayDao.selectManagerEssayByAuditing(auditing,page);
            return list;
        } else if ("0".equals(flag)) {
            Integer auditing = 0;
            PageReturn pageReturn=new PageReturn();
            page=pageReturn.ReturnPage(page);
            List<EssayAndUser> list = essayDao.selectManagerEssayByAuditing(auditing,page);
            return list;
        } else
            return null;
    }

    @Override
    public List<EssayAndUser> FindByEssayId(Integer essayId, String flag) {
        if ("0".equals(flag)) {
            Integer auditing = 0;
            return managerDao.selectByEssayId(essayId, auditing);
        } else if ("1".equals(flag)) {
            Integer auditing = 1;
            return managerDao.selectByEssayId(essayId, auditing);
        } else
            return null;
    }

    @Override
    public List<EssayAndUser> FindByTitle(String title, String flag, Page page) {
        if ("0".equals(flag)) {
            Integer auditing = 0;
            PageReturn pageReturn=new PageReturn();
            page=pageReturn.ReturnPage(page);
            return managerDao.SelectByTitle(title,auditing,page);
        } else if ("1".equals(flag)) {
            Integer auditing = 1;
            PageReturn pageReturn=new PageReturn();
            page=pageReturn.ReturnPage(page);
            return managerDao.SelectByTitle(title, auditing,page);
        } else
            return null;
    }

    @Override
    public List<EssayAndUser> FindAllByEssayId(Integer essayId) {
        return managerDao.SelectAllByEssayId(essayId);
    }

    @Override
    public List<EssayAndUser> FindAllByTitle(String title,Page page) {
        PageReturn pageReturn=new PageReturn();
        page=pageReturn.ReturnPage(page);
        return managerDao.SelectAllByTitle(title,page);
    }

    @Override
    public int FindAllEssayCount() {
        return managerDao.SelectAllEssayCount();
    }

    @Override
    public int FindAuditingEssayCount() {
        return managerDao.SelectAuditingEssayCount();
    }

    @Override
    public int FindUnAuditingEssayCount() {
        return managerDao.SelectUnAuditingEssayCount();
    }

    @Override
    public int FindByTitleCount(String title) {
        return managerDao.SelectByTitleCountAll(title);
    }

    @Override
    public int FindByTitleCountAuditing(String title) {
        return managerDao.SelectByTitleCountAuditing(title);
    }

    @Override
    public int FindByTitleCountUnAuditing(String title) {
        return managerDao.SelectByTitleCountUnAuditing(title);
    }

    @Override
    public int FindUserCount() {
        return managerDao.SelectUserCount();
    }

    @Override
    public int FindUserByNameCount(String username) {
        return managerDao.SelectUserByNameCount(username);
    }

    @Override
    public boolean ResetPassword(Manager manager) {
        int i=managerMapper.updateByPrimaryKeySelective(manager);
        if(i>0){
            return  true;
        }else
            return  false;
    }

}
